package com.han.controllers;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import com.han.bom.Exam;
import com.han.bom.PracticeExam;
import com.han.bom.Question;
import com.han.bom.RealExam;
import com.han.bom.Student;
import com.han.dao.AnswerDAO;
import com.han.dao.ExamDAO;
import com.han.dao.QuestionDAO;

/**
 * Servlet implementation class PreTakeRealExamServlet
 */
@WebServlet("/Student/PreTakeExamServlet")
public class PreTakeExamServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	private static Logger logger = Logger.getLogger(PreTakeExamServlet.class.getName());
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PreTakeExamServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		logger.info("Entered PreTakeExamServlet POST");
		
		ExamDAO examdao = new ExamDAO();
		Student student = null;
		HttpSession session = request.getSession();
		ArrayList<Exam> exams = null;
		String type = null;
		
		if(session.getAttribute("student")!=null){
			student = (Student)session.getAttribute("student");
		}
		else{
			
		}
		
		if(request.getParameter("type")!=null){
			type = request.getParameter("type");
		}
		else{
			
		}
		
		try {
			exams = examdao.retrieveExamsByStudent(student.getUserID());
		} catch (ClassNotFoundException e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		} catch (SQLException e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		
		for(int i=0;i<exams.size();i++){
			
			if(exams.get(i) instanceof RealExam && type.equals("Practice")){
				exams.remove(i);
				i--;
			}
			else if(exams.get(i) instanceof PracticeExam && type.equals("Real")){
				exams.remove(i);
				i--;
			}

		}
		
		if(type.equals("Real")){
			for(int i=0;i<exams.size();i++){
				if(exams.get(i) instanceof RealExam){
					if(!((RealExam)exams.get(i)).isActivated()){
						exams.remove(i);
						i--;
					}
				}
			}
		}
				
		RequestDispatcher rd=null;
		QuestionDAO questiondao = new QuestionDAO();
		AnswerDAO answerdao = new AnswerDAO();
		
		if(type.equals("Practice")){
			rd = request.getRequestDispatcher("Exam_Practice_Take.jsp");
		}
		else{
			rd = request.getRequestDispatcher("Exam_Real_Take.jsp");
		}
		
		for(Exam exam: exams){
			
			try {
				ArrayList<Question> questions = questiondao.retrieveQuestionsbyExam(exam.getExamID());
				
				for(Question question:questions){
					ArrayList<String> choices = answerdao.retrieveAnswersByQuestion(question.getQuestionID());
					question.setChoices(choices);
					question.setAnswer(choices.get(0));
				}
				
				exam.setQuestions(questions);
			} catch (ClassNotFoundException e) {
				logger.error(e.getMessage(), e);
				e.printStackTrace();
			} catch (SQLException e) {
				logger.error(e.getMessage(), e);
				e.printStackTrace();
			}
			
		}

		request.setAttribute("exams", exams);
		
		rd.forward(request, response);
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
